﻿
Public Class PanelEntrySetter

    'Target Node :
    Private _LocalEntrySettings As New EntrySettings
    Private _TargetNode As ISettable
    Public WriteOnly Property TargetNode As ISettable
        Set(value As ISettable)
            _TargetNode = value
            _LocalEntrySettings.CopyFrom(_TargetNode.EntrySettings)
            ShowEntrySettings()
            If TypeOf _TargetNode Is ZipNode Then
                KCB_Entry_Method.Enabled = False
                KCB_Entry_Level.Enabled = False
            Else
                KCB_Entry_Method.Enabled = True
                KCB_Entry_Level.Enabled = True
            End If
        End Set
    End Property
    'Show Entry Settings :
    Private Sub ShowEntrySettings()
        Me.SuspendLayout()
        KCB_Entry_Method.SelectedIndex = _LocalEntrySettings.Method.ToIndex
        KCB_Entry_Level.SelectedIndex = CInt(_LocalEntrySettings.Level)
        KCB_Entry_Encryption.SelectedIndex = CInt(_LocalEntrySettings.Encryption)
        KTB_Entry_Password.Text = _LocalEntrySettings.Password
        KTB_Entry_ConfirmPassword.Text = _LocalEntrySettings.Password
        KTB_Entry_Comment.Text = _LocalEntrySettings.Comment
        If KCB_Entry_Encryption.SelectedIndex <> 0 Then
            If String.Compare(KTB_Entry_Password.Text, KTB_Entry_ConfirmPassword.Text, False) = 0 Then
                ShowPasswordsWarnings(True, "Passwords Are Equivalent")
            Else
                ShowPasswordsWarnings(True, "Passwords Not Equivalent")
            End If
        Else
            KL_Entry_Warning.Visible = False
        End If
        KCB_EmbedEntrySettingsInfos.Checked = _LocalEntrySettings.EmbedEntrySettingsInfos
        Me.ResumeLayout()
    End Sub
    'Fields Sets :
    Private Sub KCB_Entry_Method_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles KCB_Entry_Method.SelectedIndexChanged
        If KCB_Entry_Method.SelectedIndex = 1 Then
            KCB_Entry_Level.Enabled = True
            KCB_Entry_Level.SelectedIndex = CInt(Zlib.CompressionLevel.Default)
        Else
            KCB_Entry_Level.Enabled = False
            KCB_Entry_Level.SelectedIndex = CInt(Zlib.CompressionLevel.None)
        End If
    End Sub
    Private Sub KCB_Entry_Level_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles KCB_Entry_Level.SelectedIndexChanged
        If KCB_Entry_Level.SelectedIndex = 0 Then
            KCB_Entry_Method.SelectedIndex = 0
        End If
    End Sub
    Private Sub KCB_Entry_Encryption_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles KCB_Entry_Encryption.SelectedIndexChanged
        If KCB_Entry_Encryption.SelectedIndex = 0 Then
            KTB_Entry_Password.Enabled = False
            KTB_Entry_ConfirmPassword.Enabled = False
            KTB_Entry_Password.Text = Nothing
            KTB_Entry_ConfirmPassword.Text = Nothing
            KL_Entry_Warning.Visible = False
        Else
            KTB_Entry_Password.Enabled = True
            KTB_Entry_ConfirmPassword.Enabled = True
            KTB_Entry_Password.Text = Nothing
            KTB_Entry_ConfirmPassword.Text = Nothing
            KL_Entry_Warning.Visible = False
            KL_Entry_Warning.Visible = False
        End If
    End Sub
    Private Sub KTB_Entry_ConfirmPassword_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KTB_Entry_ConfirmPassword.KeyUp
        If String.IsNullOrWhiteSpace(KTB_Entry_Password.Text) = True Then
            ShowPasswordsWarnings(False, "Edit Some Password")
            KTB_Entry_ConfirmPassword.Text = ""
        Else
            If String.Compare(KTB_Entry_Password.Text, KTB_Entry_ConfirmPassword.Text, False) <> 0 Then
                ShowPasswordsWarnings(False, "Passwords Not Equivalent")
            Else
                ShowPasswordsWarnings(True, "Passwords Are Equivalent")
            End If
        End If
    End Sub
    'Show Passwords Warnings :
    Private Sub ShowPasswordsWarnings(ByVal ok As Boolean, ByVal text As String)
        If ok = True Then
            KL_Entry_Warning.Values.Image = My.Resources.Img_PasswordOk
        Else
            KL_Entry_Warning.Values.Image = My.Resources.Img_PasswordError
        End If
        KL_Entry_Warning.Text = text
        KL_Entry_Warning.Visible = True
    End Sub
    'Resets :
    Private Sub BSA_Entry_ResetMethod_Click(sender As System.Object, e As System.EventArgs) Handles BSA_Entry_ResetMethod.Click
        KCB_Entry_Method.SelectedIndex = EntrySettings.DefaultSettings.Method.ToIndex
    End Sub
    Private Sub BSA_Entry_ResetLevel_Click(sender As System.Object, e As System.EventArgs) Handles BSA_Entry_ResetLevel.Click
        KCB_Entry_Level.SelectedIndex = CInt(EntrySettings.DefaultSettings.Level)
    End Sub
    Private Sub BSA_Entry_ResetEncryption_Click(sender As System.Object, e As System.EventArgs) Handles BSA_Entry_ResetEncryption.Click
        KCB_Entry_Encryption.SelectedIndex = 0
    End Sub
    Private Sub BSA_Entry_ViewPassword_Click(sender As System.Object, e As System.EventArgs) Handles BSA_Entry_ViewPassword.Click
        If BSA_Entry_ViewPassword.Checked = ButtonCheckState.Checked Then
            KTB_Entry_Password.UseSystemPasswordChar = False
            KTB_Entry_Password.PasswordChar = CChar("")
        Else
            KTB_Entry_Password.UseSystemPasswordChar = True
        End If
    End Sub
    Private Sub BSA_Entry_ViewConfirmPassword_Click(sender As System.Object, e As System.EventArgs) Handles BSA_Entry_ViewConfirmPassword.Click
        If BSA_Entry_ViewConfirmPassword.Checked = ButtonCheckState.Checked Then
            KTB_Entry_ConfirmPassword.UseSystemPasswordChar = False
            KTB_Entry_ConfirmPassword.PasswordChar = CChar("")
        Else
            KTB_Entry_ConfirmPassword.UseSystemPasswordChar = True
        End If
    End Sub
    'Actions :
    Public Sub Reset()
        _LocalEntrySettings.Reset()
        ShowEntrySettings()
    End Sub
    Public Sub Cancel()
        _LocalEntrySettings.CopyFrom(_TargetNode.EntrySettings)
        ShowEntrySettings()
    End Sub
    Public Sub Apply()
        Select Case KCB_Entry_Method.SelectedIndex
            Case 0
                _TargetNode.EntrySettings.Method = Zip.CompressionMethod.None
            Case 1
                _TargetNode.EntrySettings.Method = Zip.CompressionMethod.Deflate
            Case 2
                _TargetNode.EntrySettings.Method = Zip.CompressionMethod.BZip2
        End Select
        _TargetNode.EntrySettings.Level = CType(KCB_Entry_Level.SelectedIndex, Zlib.CompressionLevel)

        If KCB_Entry_Encryption.SelectedIndex <> 0 Then
            If String.IsNullOrWhiteSpace(KTB_Entry_Password.Text) = True Then
                _TargetNode.EntrySettings.Encryption = Zip.EncryptionAlgorithm.None
                _TargetNode.EntrySettings.Password = Nothing
            Else
                If String.Compare(KTB_Entry_Password.Text, KTB_Entry_ConfirmPassword.Text, False) <> 0 Then
                    _TargetNode.EntrySettings.Encryption = Zip.EncryptionAlgorithm.None
                    _TargetNode.EntrySettings.Password = Nothing
                Else
                    _TargetNode.EntrySettings.Encryption = CType(KCB_Entry_Encryption.SelectedIndex, Zip.EncryptionAlgorithm)
                    _TargetNode.EntrySettings.Password = KTB_Entry_Password.Text
                End If
            End If
        Else
            _TargetNode.EntrySettings.Encryption = Zip.EncryptionAlgorithm.None
            _TargetNode.EntrySettings.Password = Nothing
        End If
        _TargetNode.EntrySettings.Comment = KTB_Entry_Comment.Text
        _TargetNode.EntrySettings.EmbedEntrySettingsInfos = KCB_EmbedEntrySettingsInfos.Checked

        If TypeOf _TargetNode Is FolderNode Then
            CType(_TargetNode, FolderNode).UpdateChildsEntrySettings()
        End If
        Me.TargetNode = _TargetNode
    End Sub

End Class
